import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST;
import static org.lwjgl.opengl.GL11.GL_LEQUAL;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_NICEST;
import static org.lwjgl.opengl.GL11.GL_PERSPECTIVE_CORRECTION_HINT;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.GL_SMOOTH;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glClearColor;
import static org.lwjgl.opengl.GL11.glClearDepth;
import static org.lwjgl.opengl.GL11.glDepthFunc;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glHint;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glShadeModel;
import static org.lwjgl.opengl.GL11.glViewport;
import static org.lwjgl.util.glu.GLU.gluPerspective;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

import utils.FPSCounter;

public class Main {

	static final String TITLE = "Game Title";
	static final int WIDTH = 800;
	static final int HEIGHT = 600;
	
	private Game game;
	private FPSCounter fpsCounter;
	private boolean done = false;
	
	public Main() {
		init();

		game = new Game();
		fpsCounter = new FPSCounter();

		while (!done) {
			if (Display.isCloseRequested())
				done = true;
			render();
			Display.update();
		}
		Display.destroy();

	}

	private void render() {
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//		glLoadIdentity();
		
		game.tick();
		fpsCounter.update(TITLE);
	}

	private void init() {

		try {
			Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
			Display.setVSyncEnabled(true);
			Display.setTitle(TITLE);
			Display.create();
		} catch (Exception e) {
			System.out.println("Error setting up display");
			System.exit(0);

		}
		
		initOpenGL();

		

	}

	private void initOpenGL() {
		
//		glClearColor(0.5f, 0.5f, 0.5f, 0.0f); // sets background to grey
//		glClearDepth(1.0f); // clear depth buffer
//		glEnable(GL_DEPTH_TEST); // Enables depth testing
//		glDepthFunc(GL_LEQUAL); // sets the type of test to use for depth testing
//		glMatrixMode(GL_PROJECTION);
//		
//		glMatrixMode(GL_MODELVIEW);
//		
//		glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
		
		
		glViewport(0, 0, WIDTH, HEIGHT);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective(45.0f, ((float) WIDTH / (float) HEIGHT), 0.1f, 100.0f);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();

		glShadeModel(GL_SMOOTH);
		glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		glClearDepth(1.0f);
		glEnable(GL_DEPTH_TEST);
		glDepthFunc(GL_LEQUAL);
		glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
		
	}
	

	

	public static void main(String[] args) {
		new Main();
	}

}
